using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Xna.Framework.Media;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace MicroMsg.UI.UserContrl
{
	public class ScrollNewsPicListCtrl : UserControl
	{
		public delegate void ListItemClick(object sender, object object_0);
		private const string TAG = "ScrollNewsPicListCtrl";
		private const int cNMaxPicCnt = 9;
		private const uint cNExpandDur = 250u;
		private const uint cNAniObjMarginL = 12u;
		private const uint cNShrinkDur = 250u;
		private List<ScrollPicItem> mListImgItems;
		private Ellipse[] mArrEllipse;
		private PanoramaItem mItem1;
		private PanoramaItem mItem2;
		private static ScrollNewsPicListCtrl sInstance;
		private bool mBIsFirstLoaded = true;
		private EventWatcher mWatchxxx;
		private int mNCurImageIndex;
		private int mNCurPaIndex;
		private bool mBIsClick = true;
		private DateTime mTimeStartM;
		public bool mBIsHown;
		private int mNVisibleCnt;
		private int mNPreShowDotIndex;
		private bool mBIsExpandImgAniStarted;
		private Storyboard mSbExpandImg;
		private bool mBIsFirstIn = true;
		private bool mBIsDownLoadImgAniStarted;
		private bool mBIsShrinkImgAniStarted;
		private Storyboard mSbShrinkImg;
		private ScrollNewsPicsCtrlArgs mScrollNewsPicsCtrlArgs;
		private IApplicationBar mPreBar;
		private ApplicationBar mBar;
		internal Grid gridLayoutRoot;
		internal Panorama panoramaCtrl;
		internal PanoramaItem pItem1;
		internal ScrollPicItem img1;
		internal PanoramaItem pItem2;
		internal ScrollPicItem img2;
		internal PanoramaItem pItem3;
		internal ScrollPicItem img3;
		internal StackPanel panelDots;
		internal Ellipse dot0;
		internal Ellipse dot1;
		internal Ellipse dot2;
		internal Ellipse dot3;
		internal Ellipse dot4;
		internal Ellipse dot5;
		internal Ellipse dot6;
		internal Ellipse dot7;
		internal Ellipse dot8;
		internal Canvas canvasAniArea;
		internal Image imgAni;
		private bool _contentLoaded;
		public event ScrollNewsPicListCtrl.ListItemClick ItemClick;
		private ScrollNewsPicListCtrl()
		{
			ShowObjectInMemory.AddOneObjectRecord(this);
			this.InitializeComponent();
			this.panoramaCtrl.set_Visibility(0);
			this.panoramaCtrl.DefaultItem = this.panoramaCtrl.get_Items().get_Item(0);
			this.mItem1 = (this.panoramaCtrl.get_Items().get_Item(1) as PanoramaItem);
			this.mItem2 = (this.panoramaCtrl.get_Items().get_Item(2) as PanoramaItem);
			this.panoramaCtrl.get_Items().Remove(this.mItem1);
			this.panoramaCtrl.get_Items().Remove(this.mItem2);
			base.add_Loaded(new RoutedEventHandler(this.Control_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Control_Unloaded));
			List<ScrollPicItem> list = new List<ScrollPicItem>();
			list.Add(this.img1);
			list.Add(this.img2);
			list.Add(this.img3);
			this.mListImgItems = list;
			this.mArrEllipse = new Ellipse[]
			{
				this.dot0,
				this.dot1,
				this.dot2,
				this.dot3,
				this.dot4,
				this.dot5,
				this.dot6,
				this.dot7,
				this.dot8
			};
		}
		~ScrollNewsPicListCtrl()
		{
			ShowObjectInMemory.RemoveOneObjectRecord(this);
		}
		public static ScrollNewsPicListCtrl getInstance()
		{
			if (ScrollNewsPicListCtrl.sInstance != null)
			{
				return ScrollNewsPicListCtrl.sInstance;
			}
			ScrollNewsPicListCtrl.sInstance = new ScrollNewsPicListCtrl();
			return ScrollNewsPicListCtrl.sInstance;
		}
		private void Control_Loaded(object sender, RoutedEventArgs e)
		{
			if (!this.mBIsFirstLoaded)
			{
				return;
			}
			this.Show();
			this.mBIsFirstLoaded = true;
			Log.smethod_5("ScrollNewsPicListCtrl", "Control_Loaded");
		}
		private void Control_Unloaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("ScrollNewsPicListCtrl", "Control_Unloaded");
		}
		private void registerEvent()
		{
			if (this.mWatchxxx == null)
			{
				this.mWatchxxx = new EventWatcher(this, null, new EventHandlerDelegate(this.Onxxx));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchxxx);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchxxx != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchxxx);
				this.mWatchxxx = null;
			}
		}
		private void Onxxx(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
		}
		private void btnLayoutRoot_Click(object sender, RoutedEventArgs e)
		{
		}
		private int getScrollOffset(int nNewPIndex, int nOldPindex)
		{
			if (nNewPIndex == nOldPindex)
			{
				return 0;
			}
			int num = nNewPIndex - nOldPindex;
			if (num != -1)
			{
				if (num != 1)
				{
					if (num != -2)
					{
						if (num != 2)
						{
							return 0;
						}
					}
					return num / -2;
				}
			}
			return num;
		}
		private void panoramaCtrl_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			int selectedIndex = this.panoramaCtrl.SelectedIndex;
			if (selectedIndex == this.mNCurPaIndex)
			{
				return;
			}
			int num = this.mNCurImageIndex + this.getScrollOffset(selectedIndex, this.mNCurPaIndex);
			if (num >= 0 && num < this.mScrollNewsPicsCtrlArgs.mListSnsImg.get_Count())
			{
				this.mNCurImageIndex = num;
				base.get_Dispatcher().BeginInvoke(delegate
				{
					this.setImgToPaCtrl();
				});
				this.mNCurPaIndex = selectedIndex;
				return;
			}
			this.panoramaCtrl.DefaultItem = this.panoramaCtrl.get_Items().get_Item(this.mNCurPaIndex);
		}
		private void panoramaCtrl_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
		{
			double totalMilliseconds = DateTime.get_Now().Subtract(this.mTimeStartM).get_TotalMilliseconds();
			if (totalMilliseconds < 250.0)
			{
				this.mTimeStartM = DateTime.get_Now();
				this.mBIsClick = false;
				return;
			}
			this.mTimeStartM = DateTime.get_Now();
			this.mBIsClick = true;
		}
		private void panoramaCtrl_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
		{
			Point translation = e.get_DeltaManipulation().get_Translation();
			if (translation.get_X() * translation.get_X() + translation.get_Y() * translation.get_Y() > 25.0)
			{
				this.mBIsClick = false;
			}
			if (this.mNCurImageIndex == 0 && e.get_DeltaManipulation().get_Translation().get_X() > 0.0)
			{
				e.set_Handled(true);
				e.Complete();
			}
			if (this.mNCurImageIndex == this.mScrollNewsPicsCtrlArgs.mListSnsImg.get_Count() - 1 && e.get_DeltaManipulation().get_Translation().get_X() < 0.0)
			{
				e.set_Handled(true);
				this.mTimeStartM = DateTime.get_Now();
				e.Complete();
			}
		}
		private void panoramaCtrl_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
		{
			if (this.mBIsClick)
			{
				double totalMilliseconds = DateTime.get_Now().Subtract(this.mTimeStartM).get_TotalMilliseconds();
				if (totalMilliseconds < 250.0)
				{
					this.Hide();
				}
			}
		}
		private void btnPItem_Click(object sender, RoutedEventArgs e)
		{
		}
		public void Start()
		{
			if (ScrollNewsPicsCtrlArgs.CheckIsInfoOk(this.mScrollNewsPicsCtrlArgs))
			{
				this.setAppBar();
				Panel mPanelHolder = this.mScrollNewsPicsCtrlArgs.mPanelHolder;
				if (mPanelHolder.get_Children().Contains(this))
				{
					return;
				}
				if (base.get_Parent() != null)
				{
					return;
				}
				this.mScrollNewsPicsCtrlArgs.mPanelHolder.get_Children().Add(this);
				this.setVisibleDotCnt(this.mScrollNewsPicsCtrlArgs.mListSnsImg.get_Count());
				this.showDotIndex(this.mScrollNewsPicsCtrlArgs.mNFirstPicIndex);
				BasePage topPage = PageManager.getInstance().GetTopPage();
				topPage.add_BackKeyPress(new EventHandler<CancelEventArgs>(this.page_BackKeyPress));
			}
		}
		private void page_BackKeyPress(object sender, CancelEventArgs e)
		{
			if (!this.mBIsShrinkImgAniStarted && !this.mBIsExpandImgAniStarted)
			{
				this.Hide();
				return;
			}
			e.set_Cancel(true);
		}
		public void Show()
		{
			this.mBIsHown = true;
			this.initUIByArgs();
		}
		private void setImgToPaCtrl()
		{
			int selectedIndex = this.panoramaCtrl.SelectedIndex;
			int count = this.mScrollNewsPicsCtrlArgs.mListSnsImg.get_Count();
			int num = (selectedIndex - 1 >= 0) ? (selectedIndex - 1) : 2;
			int num2 = (selectedIndex + 1 < 3) ? (selectedIndex + 1) : 0;
			this.showDotIndex(this.mNCurImageIndex);
			if (this.mNCurImageIndex - 1 >= 0)
			{
				this.mListImgItems.get_Item(num).set_Opacity(1.0);
				this.mListImgItems.get_Item(num).ObjMediaInfo = this.mScrollNewsPicsCtrlArgs.mListSnsImg.get_Item(this.mNCurImageIndex - 1);
			}
			else
			{
				this.mListImgItems.get_Item(num).set_Opacity(0.001);
			}
			if (this.mNCurImageIndex + 1 < count)
			{
				this.mListImgItems.get_Item(num2).set_Opacity(1.0);
				this.mListImgItems.get_Item(num2).ObjMediaInfo = this.mScrollNewsPicsCtrlArgs.mListSnsImg.get_Item(this.mNCurImageIndex + 1);
				return;
			}
			this.mListImgItems.get_Item(num2).set_Opacity(0.001);
		}
		public void Hide()
		{
			if (!this.mBIsHown)
			{
				return;
			}
			this.mBIsHown = false;
			BasePage topPage = PageManager.getInstance().GetTopPage();
			topPage.remove_BackKeyPress(new EventHandler<CancelEventArgs>(this.page_BackKeyPress));
			this.setVisibleDotCnt(0);
			this.startShrinkImgAni();
		}
		private void initUIByArgs()
		{
			if (this.mScrollNewsPicsCtrlArgs == null)
			{
				return;
			}
			if (!ScrollNewsPicsCtrlArgs.CheckIsInfoOk(this.mScrollNewsPicsCtrlArgs))
			{
				return;
			}
			this.mNCurImageIndex = this.mScrollNewsPicsCtrlArgs.mNFirstPicIndex;
			if (this.imagesOk())
			{
				this.startExpandImgAni();
				return;
			}
			this.startDownLoadImgAni();
		}
		private bool imagesOk()
		{
			return true;
		}
		private void setVisibleDotCnt(int nCnt)
		{
			if (nCnt > 1 && nCnt <= 9)
			{
				int num = this.mNVisibleCnt;
				for (int i = 0; i < num; i++)
				{
					this.mArrEllipse[i].set_Visibility(1);
				}
				for (int j = 0; j < nCnt; j++)
				{
					this.mArrEllipse[j].set_Visibility(0);
				}
				this.panelDots.set_Visibility(0);
				this.mNVisibleCnt = nCnt;
				return;
			}
			this.panelDots.set_Visibility(1);
		}
		private void showDotIndex(int nIndex)
		{
			if (nIndex >= 0 && nIndex < this.mNVisibleCnt)
			{
				Shape arg_3D_0 = this.mArrEllipse[this.mNPreShowDotIndex];
				SolidColorBrush solidColorBrush = new SolidColorBrush();
				solidColorBrush.set_Color(Color.FromArgb(119, 255, 255, 255));
				arg_3D_0.set_Fill(solidColorBrush);
				Shape arg_70_0 = this.mArrEllipse[nIndex];
				SolidColorBrush solidColorBrush2 = new SolidColorBrush();
				solidColorBrush2.set_Color(Color.FromArgb(255, 255, 255, 255));
				arg_70_0.set_Fill(solidColorBrush2);
				this.mNPreShowDotIndex = nIndex;
				return;
			}
		}
		private void startExpandImgAni()
		{
			this.canvasAniArea.set_Visibility(0);
			Panel arg_24_0 = this.gridLayoutRoot;
			SolidColorBrush solidColorBrush = new SolidColorBrush();
			solidColorBrush.set_Color(Colors.get_Black());
			arg_24_0.set_Background(solidColorBrush);
			Panel arg_41_0 = this.canvasAniArea;
			SolidColorBrush solidColorBrush2 = new SolidColorBrush();
			solidColorBrush2.set_Color(Colors.get_Black());
			arg_41_0.set_Background(solidColorBrush2);
			this.imgAni.set_Visibility(0);
			this.imgAni.set_Stretch(this.img1.imgContent.get_Stretch());
			this.mSbExpandImg = new Storyboard();
			this.panoramaCtrl.set_IsEnabled(false);
			this.panoramaCtrl.set_IsHitTestVisible(false);
			base.get_Dispatcher().BeginInvoke(delegate
			{
				this.panoramaCtrl.set_IsEnabled(true);
				this.panoramaCtrl.set_IsHitTestVisible(true);
			});
			this.mSbExpandImg.Begin();
			this.mSbExpandImg.add_Completed(new EventHandler(this.mSbExpandImg_Completed));
			this.mBIsExpandImgAniStarted = true;
		}
		private void mSbExpandImg_Completed(object sender, EventArgs e)
		{
			this.stopExpandImgAni();
		}
		private void stopExpandImgAni()
		{
			if (this.mBIsFirstIn)
			{
				this.mNCurPaIndex = 0;
				this.panoramaCtrl.get_Items().Add(this.mItem1);
				this.panoramaCtrl.get_Items().Add(this.mItem2);
				this.mBIsFirstIn = false;
			}
			else
			{
				this.mNCurPaIndex = this.panoramaCtrl.SelectedIndex;
			}
			if (this.mListImgItems == null || this.mListImgItems.get_Count() <= 0)
			{
				return;
			}
			if (this.mNCurPaIndex < 0)
			{
				Log.smethod_5("ScrollNewsPicListCtrl", "stopExpandImgAni panoramaCtrl.SelectedIndex < 0");
				this.panoramaCtrl.DefaultItem = this.mListImgItems.get_Item(0);
				this.mNCurPaIndex = 0;
			}
			this.mBIsExpandImgAniStarted = false;
			if (this.mSbExpandImg != null)
			{
				this.mSbExpandImg.Stop();
				this.mSbExpandImg = null;
			}
			if (!ScrollNewsPicsCtrlArgs.CheckIsInfoOk(this.mScrollNewsPicsCtrlArgs))
			{
				return;
			}
			this.mListImgItems.get_Item(this.mNCurPaIndex).ObjMediaInfo = this.mScrollNewsPicsCtrlArgs.mListSnsImg.get_Item(this.mScrollNewsPicsCtrlArgs.mNFirstPicIndex);
			this.panoramaCtrl.set_Visibility(0);
			this.setImgToPaCtrl();
			this.imgAni.set_Visibility(1);
			this.canvasAniArea.set_Visibility(1);
		}
		private void startDownLoadImgAni()
		{
			this.mBIsDownLoadImgAniStarted = true;
		}
		private void stopDownLoadImgAni()
		{
			this.mBIsDownLoadImgAniStarted = false;
		}
		private void startShrinkImgAni()
		{
			if (this.mBIsShrinkImgAniStarted)
			{
				return;
			}
			this.canvasAniArea.set_Visibility(0);
			Panel arg_2D_0 = this.gridLayoutRoot;
			SolidColorBrush solidColorBrush = new SolidColorBrush();
			solidColorBrush.set_Color(Colors.get_Transparent());
			arg_2D_0.set_Background(solidColorBrush);
			Panel arg_4A_0 = this.canvasAniArea;
			SolidColorBrush solidColorBrush2 = new SolidColorBrush();
			solidColorBrush2.set_Color(Colors.get_Transparent());
			arg_4A_0.set_Background(solidColorBrush2);
			this.imgAni.set_Visibility(0);
			this.panoramaCtrl.set_Visibility(1);
			this.imgAni.set_Stretch(2);
			this.stopShrinkImgAni();
		}
		private void mSbShrinkImg_Completed(object sender, EventArgs e)
		{
			this.stopShrinkImgAni();
		}
		private void stopShrinkImgAni()
		{
			this.mBIsShrinkImgAniStarted = false;
			if (this.mSbShrinkImg != null)
			{
				this.mSbShrinkImg.Stop();
				this.mSbShrinkImg = null;
			}
			this.imgAni.set_Visibility(1);
			this.canvasAniArea.set_Visibility(1);
			this.mScrollNewsPicsCtrlArgs.mPanelHolder.get_Children().Remove(this);
			if (this.mScrollNewsPicsCtrlArgs.mDlgHide != null)
			{
				this.mScrollNewsPicsCtrlArgs.mDlgHide(null, null);
			}
			this.mScrollNewsPicsCtrlArgs.mDlgHide = null;
			this.mScrollNewsPicsCtrlArgs.mDlgShow = null;
			this.mScrollNewsPicsCtrlArgs.mListSnsImg = null;
			this.mScrollNewsPicsCtrlArgs.mNFirstPicIndex = -1;
			this.mScrollNewsPicsCtrlArgs.mPicsCtrl = null;
			this.mScrollNewsPicsCtrlArgs.mPanelHolder = null;
			this.mScrollNewsPicsCtrlArgs = null;
			this.recoverAppBar();
		}
		public void SetArgs(ScrollNewsPicsCtrlArgs scrollPicCtrArg)
		{
			this.mScrollNewsPicsCtrlArgs = scrollPicCtrArg;
			this.mBIsFirstLoaded = true;
		}
		private void setAppBar()
		{
			BasePage topPage = PageManager.getInstance().GetTopPage();
			if (topPage == null)
			{
				return;
			}
			this.mPreBar = topPage.get_ApplicationBar();
			this.mBar = new ApplicationBar();
			ApplicationBarMenuItem applicationBarMenuItem = new ApplicationBarMenuItem(strings.COM_Save);
			applicationBarMenuItem.add_Click(new EventHandler(this.btnSave_Click));
			this.mBar.get_MenuItems().Add(applicationBarMenuItem);
			topPage.set_ApplicationBar(this.mBar);
			BasePage.SetAppBarStyle(this.mBar);
		}
		private string GetExisPicName()
		{
			return "Image" + DateTime.get_Now().get_Ticks() + ".jpg";
		}
		private void btnSave_Click(object sender, EventArgs e)
		{
			if (ScrollNewsPicsCtrlArgs.CheckIsInfoOk(this.mScrollNewsPicsCtrlArgs) && this.mScrollNewsPicsCtrlArgs.mListSnsImg.get_Count() > this.mNCurImageIndex && this.mNCurImageIndex >= 0)
			{
				try
				{
					MediaLibrary mediaLibrary = new MediaLibrary();
					byte[] array = SnsMediaStorage.load(this.mScrollNewsPicsCtrlArgs.mListSnsImg.get_Item(this.mNCurImageIndex).strUrl);
					Stream stream = new MemoryStream(array);
					mediaLibrary.SavePicture(this.GetExisPicName(), stream);
					stream.Seek(0L, 0);
					MessageBoxManager.ShowToast(strings.ImageViewSaveSuccess, 2500, null, false);
				}
				catch (Exception ex)
				{
					Log.smethod_1("ScrollNewsPicListCtrl", "SavaPic Exception" + ex.get_StackTrace());
					MMMessageBox.Show(strings.ImageViewSaveFail, true, "确定");
				}
				return;
			}
			Log.smethod_1("ScrollNewsPicListCtrl", "SavaPic failed check parameters failed");
			MMMessageBox.Show(strings.ImageViewSaveFail, true, "确定");
		}
		private void recoverAppBar()
		{
			BasePage topPage = PageManager.getInstance().GetTopPage();
			if (topPage == null)
			{
				return;
			}
			topPage.set_ApplicationBar(this.mPreBar);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/TXNews/UI/ScrollNewsPicListCtrl.xaml", 2));
			this.gridLayoutRoot = (Grid)base.FindName("gridLayoutRoot");
			this.panoramaCtrl = (Panorama)base.FindName("panoramaCtrl");
			this.pItem1 = (PanoramaItem)base.FindName("pItem1");
			this.img1 = (ScrollPicItem)base.FindName("img1");
			this.pItem2 = (PanoramaItem)base.FindName("pItem2");
			this.img2 = (ScrollPicItem)base.FindName("img2");
			this.pItem3 = (PanoramaItem)base.FindName("pItem3");
			this.img3 = (ScrollPicItem)base.FindName("img3");
			this.panelDots = (StackPanel)base.FindName("panelDots");
			this.dot0 = (Ellipse)base.FindName("dot0");
			this.dot1 = (Ellipse)base.FindName("dot1");
			this.dot2 = (Ellipse)base.FindName("dot2");
			this.dot3 = (Ellipse)base.FindName("dot3");
			this.dot4 = (Ellipse)base.FindName("dot4");
			this.dot5 = (Ellipse)base.FindName("dot5");
			this.dot6 = (Ellipse)base.FindName("dot6");
			this.dot7 = (Ellipse)base.FindName("dot7");
			this.dot8 = (Ellipse)base.FindName("dot8");
			this.canvasAniArea = (Canvas)base.FindName("canvasAniArea");
			this.imgAni = (Image)base.FindName("imgAni");
		}
	}
}
